package com.chima.dao;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chima.domain.Department;
import com.chima.domain.JobDetail;
import com.chima.domain.Resource;

public class ResourceDao extends HibernateDaoSupport {

	public List<Resource> resourcesfindbyroseid(int id) {
		Session session=this.getSession();
	   String sql="select * from t_resource where id in(select resourceID from t_rose_resource where roseID=?)";
		List<Resource> l = ((SQLQuery)session.createSQLQuery(sql).setParameter(0, id)).addEntity(Resource.class).list();
		super.releaseSession(session);
		return l;
	}

	public List<Resource> resourcesfindreResources(int pid) {
		Session session=this.getSession();
		String sql="select * from t_resource where pid=?";
		List<Resource> l = ((SQLQuery) session.createSQLQuery(sql).setParameter(0, pid)).addEntity(Resource.class).list();
		super.releaseSession(session);
		return l;
		
	}

	public Resource findidbyname(String string) {
		Session session=this.getSession();
		Resource d = (Resource)session.createQuery("from Resource where name=?").setParameter(0, string).uniqueResult();
		super.releaseSession(session);
		return d;
	}

	public void deleteresource(int id,int rsid) {
		Session session=this.getSession();
		String hqlString="delete  from t_rose_resource where roseID=? and resourceID=? ";
		session.createSQLQuery(hqlString).setInteger(0, id).setInteger(1, rsid).executeUpdate();
		super.releaseSession(session);
	}

	public List<Resource> ListALL() {
		Session session=this.getSession();
		String sql="select * from t_resource";
		List<Resource> l = ((SQLQuery) session.createSQLQuery(sql)).addEntity(Resource.class).list();
		super.releaseSession(session);
		return l;
		
	}

	public Resource findbyId(int id) {
		Session session=this.getSession();
		Resource d = (Resource)session.createQuery("from Resource where id=?").setParameter(0, id).uniqueResult();
		super.releaseSession(session);
		return d;
	}

}
